package com.lkd.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkd.entity.Classify;
import com.lkd.entity.Sku;
import com.lkd.mapper.ClassifyMapper;
import com.lkd.mapper.SkuMapper;
import com.lkd.service.SkuService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkd.vo.PageBean;
import com.lkd.vo.SkuVo;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-19
 */
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {

    @Autowired
    private ClassifyMapper classifyMapper;

    @Override
    public PageBean<SkuVo> selectByPage (String skuName, Integer pageIndex, Integer pageSize) {
        Page<Sku> page = new Page<> (pageIndex, pageSize);

        LambdaQueryWrapper<Sku> wrapper = new LambdaQueryWrapper<> ();
        wrapper.like (StrUtil.isNotEmpty (skuName),Sku::getSkuName,skuName);

        page = getBaseMapper ().selectPage (page, wrapper);

        List<SkuVo> skuVoList = page.getRecords ().stream ().map (sku -> {

            SkuVo skuVo = BeanUtil.copyProperties (sku, SkuVo.class);
            Integer classId = sku.getClassId ();

            Classify classify = classifyMapper.selectById (classId);
            skuVo.setSkuClass (classify);

            return skuVo;
        }).collect (Collectors.toList ());

        return new PageBean<> (page, skuVoList);
    }
}
